//
//  CZHomeTableViewCell.swift
//  sinaSwifWeiBo
//
//  Created by 张雪雪 on 16/3/3.
//  Copyright © 2016年 张雪雪. All rights reserved.
//

import UIKit
import SnapKit

class CZHomeTableViewCell: UITableViewCell {

    //定义一个约束来记录底部的约束
    var constant : Constraint?
    
    
    var statusViewModel : CZStatusViewModel?{
    
        didSet{
        
        
            orginalView.statusViewModel = statusViewModel
            footerView.footerViewModel = statusViewModel
            
// MARK:--注意cell重用的问题
              constant?.uninstall()
            //MARK:判断是否有转发微博
            
            if statusViewModel?.statusModel?.retweeted_status == nil{
            
            retweetView.hidden = true
                //更新一下底部约束
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constant = make.top.equalTo(orginalView.snp_bottom).constraint
                
                })
            
            
            }else{
            
            retweetView.hidden = false
                
               retweetView.retweetViewModel = statusViewModel
            //更新约束
                footerView.snp_updateConstraints(closure: { (make) -> Void in
                    self.constant = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            
            }
            
            
        
        }
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
    
    //添加到视图里
        contentView.addSubview(orginalView)
        contentView.addSubview(footerView)
        contentView.addSubview(retweetView)
        
        
    //添加约束左,上，由下
        orginalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            //make.bottom.equalTo(contentView.snp_bottom)
            
        }
        
        retweetView.snp_makeConstraints { (make) -> Void in
           make.left.equalTo(contentView.snp_left)
            //注意
            make.top.equalTo(orginalView.snp_bottom)
            make.right.equalTo(contentView.snp_right)
        }
        
        
        footerView.snp_makeConstraints { (make) -> Void in
            
            self.constant = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.left.equalTo(contentView.snp_left)
//            make.top.equalTo(retweetView.snp_bottom)
            make.right.equalTo(contentView.snp_right)
            make.bottom.equalTo(contentView.snp_bottom)
            
            //设置高度
            make.height.equalTo(40)
        }

        //什么把视图,放值到contentView
        /*
        contentView是根据自动计算的左右上下
        一定注意
        */

    
    contentView.snp_makeConstraints { (make) -> Void in
        make.left.equalTo(self.snp_left)
        make.top.equalTo(self.snp_top)
        make.right.equalTo(self.snp_right)
        make.bottom.equalTo(footerView.snp_bottom)
        
        }
    
    
    }
    
    private lazy var orginalView:CZOrginal = CZOrginal()

    private lazy var footerView:CZFooterView = CZFooterView()
    
    private lazy var retweetView:CZRetweetView = CZRetweetView()

}
